var app = getApp();
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        curPage : "/zofui_sales/card/mylist",
        tabs : [{name:'全部卡券'},{name:'在线使用'},{name:'到店使用'}],
        page : {
            isend : false,
            doing : false,
            doo : 'card',
            pdata : {
                op : 'mylist',
                page : 1,
                type : 0,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        options : {},
    },
    onLoad:function(options){
        var self = this;
        this.data.options = options;

        var zfid = options.zfid ? options.zfid : '';
		var shopid = options.shopid ? options.shopid : 0;
        self.setData({
            'page.pdata.zfid' : zfid,
        });
        
        app.com.auth(function(user){
            app.com.getPage(self,self.data.page,20,function(res){
                var data = res.data.data;
                self.setData({
                   datalist : data.list,
                   params : res.data.data.params,
                   sharedata : res.data.data.sharedata,
                },function(){
                    self.setData({
                        inited : true,
                    });
                });

                wx.setNavigationBarTitle({
                  title:  res.data.data.params.title ? res.data.data.params.title : '我的优惠券',
                });

            },false,function(res){
                app.com.alert(res.data.message);
            });
			app.com.setBar(app,self,shopid);
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.setData({
            isdown : true,
            'page.pdata.page' : 1,
            'page.isend' : false,
        });
        
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });
        wx.stopPullDownRefresh();
    },
    switch : function(e){
        var self = this;
        if( self.data.ising || e.detail.name == self.data.page.pdata.status  ) return false;
        self.data.ising = true;

        self.setData({
            'page.pdata.type' : e.detail.name,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });

        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });

        self.data.ising = false;
    },
    useit(e){
        var self = this;
        app.com.http('cut','POST',{cid:e.currentTarget.dataset.id,op:'getoid'},0,true,'','',function(res){
            if (!res.data.errno && res.data ) {
                var tourl = '/zofui_sales/order/orderinfo?id='+res.data.data.orderid;
                wx.navigateTo({
                  url: tourl
                });
            }else{
                app.com.alert(res.data.message);
            }
        })
    },
    payit(e){
        // 4 砍价订单
        wx.setStorageSync('zfssbuy',{'cid':e.currentTarget.dataset.id,type:4});
        var tourl = '/zofui_sales/order/confirm';
        wx.navigateTo({
          url: tourl
        });
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    location : function( e ){
        app.com.location( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e);
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    }
    

})